<?php

namespace App\Admin\Controllers;

use App\Video;
use App\VideoChannel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class VideoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'youtube视频';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Video());

        $grid->column('id', __('ID'))->sortable();
        $grid->column('name', __('视频名称'));
        $grid->column('videoChannel.name', __('频道名称'));
        $grid->column('updated_at', __('Updated at'));

        $grid->filter(function($filter){
            $filter->like('videoChannel.name','频道名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Video::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('name', __('视频名称'));
        $show->url('视频地址')->unescape()->as(function ($url){

            return "<video controls src='{$url}'></video>";
        });
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Video());

        $video_channel = VideoChannel::all();

        $video_channel_id_options = array();
        foreach ($video_channel as $item) {
            $video_channel_id_options[$item->id] = $item->name;
        }

        $form->display('id', __('ID'));
        $form->text('name', __('视频名称'));
        $form->text('url', __('视频地址'));
        $form->select('video_channel_id', '视频频道')->options($video_channel_id_options);
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        return $form;
    }
}
